package com.aisier.architecture.base

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.LiveData
import com.gyf.immersionbar.ImmersionBar
import com.zzq.my.jetpacks.base.R
import com.zzq.my.jetpacks.util.ActivityStackManager
import com.zzq.my.jetpacks.util.LoadingDialogs
import com.zzq.my.jetpacks.util.viewLifeCycleOwner

/*
  * ================================================
  * 描述：抽象类BaseActivity
  * 作者：zhuangzeqin
  * 时间: 2021/11/5-16:13
  * 邮箱：zzq@eeepay.cn
  * 备注:
  * ----------------------------------------------------------------
  * You never know what you can do until you try !
  *      _              _           _     _   ____  _             _ _
  *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
  *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
  *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
  *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
  *
  * 签名：最痛苦的事不是我失败了，而是我本可以.--zzq
  * ----------------------------------------------------------------
  * ================================================
  */
abstract class BaseActivity(@LayoutRes contentLayoutId: Int) : AppCompatActivity(contentLayoutId),
    IUiView {
    companion object {
        val TAG: String by lazy { BaseActivity::class.java.simpleName.toString() }
    }

    /* ------注释说明---加载弹层----- */
    private var dialog: Dialog? = null

    /***上下文对象 */
    protected lateinit var mContext: Context

    /***用于传递数据对象 */
    protected var mBundle: Bundle? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ActivityStackManager.getInstance().push(this)
        this.mContext = this
        mBundle = intent.extras
        ImmersionBar.with(this).statusBarColor(R.color.purple_500).init()
        /** 设置初始化 View **/
        initView()
        /** 设置点击事件操作**/
        eventOnClick()
        /** 初始化数据**/
        initData()
    }

    /**
     * 扩展liveData的observe函数
     */
    protected fun <T : Any> LiveData<T>.observerKt(block: (T?) -> Unit) {
        this.observe(viewLifeCycleOwner, {
            block.invoke(it)
        })

    }


    /**
     * 显示加载弹框
     * cancelable
     */
    override fun showLoading(msg: String, cancelable: Boolean) {
        if (!isFinishing) {
            if (dialog == null) {
                dialog = LoadingDialogs.createLoadingDialog(mContext, msg)
                with(dialog!!) {
                    //点击空白取消
                    setCanceledOnTouchOutside(cancelable)
                    setCancelable(cancelable)
                }
            }
            dialog?.show()
        }
    }

    /**
     * 关闭弹层
     */
    override fun dismissLoading() {
        dialog?.takeIf { it.isShowing }?.dismiss()
    }

    override fun onDestroy() {
        super.onDestroy()
        ActivityStackManager.getInstance().remove(this)
    }

    /**
     * 初始化数据
     */
    protected abstract fun initData()

    /**
     * 初始化操作
     */
    protected abstract fun initView()

    /**
     * 初始化点击事件操作
     */
    protected abstract fun eventOnClick()


}